/*
 * Dot.cpp
 *
 *  Created on: Jan 1, 2014
 *      Author: nick
 */

#include "Dot.h"

Dot::Dot()
{
	box.x = 0;
	box.y = 0;

	jumpHeight = 10;
	wallGrab = 0;

	jumping = 0;
	};

Dot::~Dot(){};

void Dot::jump() {
	if (jumping == jumpHeight)
		yVel -= DOT_HEIGHT;
	else if (jumping == 1)
		yVel += DOT_HEIGHT;

	if (jumping > 0)
		--jumping;
}

void Dot::handleInput() {

	//If a key was pressed
	if (event.type == SDL_KEYDOWN) {
		//Adjust the velocity
		switch (event.key.keysym.sym) {
		case SDLK_SPACE:
		case SDLK_UP:
			if (jumping == 0)
				jumping = jumpHeight;
			break;
		case SDLK_DOWN:
			break;
		case SDLK_LEFT:
			xVel -= DOT_WIDTH / 2;
			wallGrab += 1;
			break;
		case SDLK_RIGHT:
			xVel += DOT_WIDTH / 2;
			wallGrab += 1;
			break;
		default:
			break;
		}
		std::cout << "\nOn key " << event.key.keysym.sym << " press yVel: "
				<< yVel;
	}
	//If a key was released
	else if (event.type == SDL_KEYUP) {
		//Adjust the velocity
		switch (event.key.keysym.sym) {
		case SDLK_SPACE:
		case SDLK_UP:
			if (jumping > 0)
				yVel += DOT_HEIGHT;
			jumping = -1;
			break;
		case SDLK_DOWN:
			//yVel -= DOT_HEIGHT / 2;
			break;
		case SDLK_LEFT:
			xVel += DOT_WIDTH / 2;
			wallGrab -= 1;
			break;
		case SDLK_RIGHT:
			xVel -= DOT_WIDTH / 2;
			wallGrab -= 1;
			break;
		default:
			break;
		}
		std::cout << "\nOn key " << event.key.keysym.sym << " release yVel: "
				<< yVel;
	}
}

void Dot::set_camera() {
	//Center the camera over the dot
	camera->x = (box.x + DOT_WIDTH / 2) - SCREEN_WIDTH / 2;
	camera->y = (box.y + DOT_HEIGHT / 2) - SCREEN_HEIGHT / 2;

	//Keep the camera in bounds.
	if (camera->x < 0) {
		camera->x = 0;
	}
	if (camera->y < 0) {
		camera->y = 0;
	}
	if (camera->x > LEVEL_WIDTH - camera->w) {
		camera->x = LEVEL_WIDTH - camera->w;
	}
	if (camera->y > LEVEL_HEIGHT - camera->h) {
		camera->y = LEVEL_HEIGHT - camera->h;
	}
}

void Dot::move(Sprite * tiles[]){
	//Move the dot left or right
	box.x += xVel;

	for (int i = 0; i < TOTAL_TILES; ++i) {
		if( tiles[i]->getType() == TILE_WALL )
		//If the dot went too far to the left or right or touched a wall
		if ((box.x < 0.0) || (box.x + box.w > LEVEL_WIDTH)
				|| tiles[i]->isWithin(box.x, box.y)) {
			//move back
			box.x -= xVel;
		}
	}

	//Move the dot up or down
	box.y += yVel;

	for (int i = 0; i < TOTAL_TILES; ++i) {
		//If the dot went too far up or down or touched a wall
		if ((box.y < 0) || (box.y + box.h > LEVEL_HEIGHT)
				|| tiles[i]->isWithin(box.x, box.y)) {
			//move back
			box.y -= yVel;
		}
	}
};
